package com.yyydjk.library.activitys;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.mikepenz.iconics.IconicsDrawable;
import com.mikepenz.material_design_iconic_typeface_library.MaterialDesignIconic;
import com.yyydjk.library.R;
import com.yyydjk.library.base.BaseActivity;
import com.yyydjk.library.event.SkinChangeEvent;
import com.yyydjk.library.theme.ColorView;
import com.yyydjk.library.utils.SystemUtils;
import com.yyydjk.library.utils.ThemeUtils;
import com.yyydjk.library.value.AppValue;
import com.yyydjk.library.value.NetValue;
import com.yyydjk.library.widget.MultipleStatusView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.Arrays;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import es.dmoral.toasty.Toasty;

/**
 * Created by Administrator on 2018/4/9 0009.
 */

public class LibraryListActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener{
    @Bind(R.id.status_bar)
    ColorView mStatusBar;
    @Bind(R.id.icon)
    ImageView mIcon;
    @Bind(R.id.title)
    TextView mTitle;
    @Bind(R.id.multipleStatusView)
    MultipleStatusView multipleStatusView;
    @Bind(R.id.content_view)
    SwipeRefreshLayout swipeRefreshLayout;
    @Bind(R.id.swipe_target)
    RecyclerView recyclerView;
    BaseQuickAdapter baseQuickAdapter;
    private String Bartitle;
    private String librarytype;
    private String classType;
    private List<AVObject> datas;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_librarylist);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            mStatusBar.setVisibility(View.VISIBLE);
            mStatusBar.getLayoutParams().height = SystemUtils.getStatusHeight(this);
            mStatusBar.setLayoutParams(mStatusBar.getLayoutParams());
        } else {
            mStatusBar.setVisibility(View.GONE);
        }
        initListener();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        multipleStatusView.showLoading();
        initDatas();
    }

    private void initListener() {
        swipeRefreshLayout.setOnRefreshListener(this);
        multipleStatusView.setOnRetryClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                multipleStatusView.showLoading();
                initNetDatas();
            }
        });
    }

    private void initDatas() {
        Intent intent = getIntent();
        Bartitle = intent.getStringExtra(AppValue.LIBRARYLISTACTIVITY_TITLE);
        librarytype = intent.getStringExtra(AppValue.LIBRARYLISTACTIVITY_LIBRARYTYPE);
        classType = intent.getStringExtra(AppValue.LIBRARYLISTACTIVITY_CLASSTYPE);
        mTitle.setText(Bartitle);
        switch (classType){
            case "Android":
                mIcon.setImageDrawable(new IconicsDrawable(this).color(Color.WHITE).icon(MaterialDesignIconic.Icon.gmi_android).sizeDp(20));
                break;
            case "App":
                mIcon.setImageDrawable(new IconicsDrawable(this).color(Color.WHITE).icon(MaterialDesignIconic.Icon.gmi_apps).sizeDp(20));
                break;
            case "QD":
                mIcon.setImageDrawable(new IconicsDrawable(this).color(Color.WHITE).icon(MaterialDesignIconic.Icon.gmi_language_javascript).sizeDp(20));
                break;
            case "JiaoCheng":
                mIcon.setImageDrawable(new IconicsDrawable(this).color(Color.WHITE).icon(MaterialDesignIconic.Icon.gmi_book).sizeDp(20));
                break;
        }
        initNetDatas();
    }
    private void initNetDatas(){
        //        获取服务器数据
        final AVQuery<AVObject> startDateQuery = new AVQuery<>(NetValue.MLIBRARY);
        startDateQuery.whereEqualTo(NetValue.MLIBRARY_CLASSTYPE, classType);

        final AVQuery<AVObject> endDateQuery = new AVQuery<>(NetValue.MLIBRARY);
        endDateQuery.whereEqualTo(NetValue.MLIBRARY_LIBRARYTYPE, librarytype);

        AVQuery<AVObject> query = AVQuery.and(Arrays.asList(startDateQuery, endDateQuery));
        query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> list, AVException e) {
                if(e==null){
                    datas=list;
                    if(datas.size()>0){
                        if(baseQuickAdapter==null){
                            initAdapter();
                        }else {
                            baseQuickAdapter.setNewData(datas);
                            baseQuickAdapter.notifyDataSetChanged();
                        }
                        multipleStatusView.showContent();
                        swipeRefreshLayout.setRefreshing(false);
                    }else {
                        multipleStatusView.showEmpty();
                    }

                }else {
                    multipleStatusView.showNoNetwork();
                }
            }
        });
    }

    private void initAdapter() {
        baseQuickAdapter=new BaseQuickAdapter<AVObject,BaseViewHolder>(R.layout.item_common_librarylist,datas) {

            @Override
            protected void convert(BaseViewHolder helper, AVObject item) {
                helper.addOnClickListener(R.id.img_desc);
                TextView textView = helper.getView(R.id.text);
               helper.setImageDrawable(R.id.img_desc,new IconicsDrawable(LibraryListActivity.this)
                       .icon(MaterialDesignIconic.Icon.gmi_help_outline)
                       .color(ThemeUtils.getThemeColor(LibraryListActivity.this, R.attr.colorPrimary))
                       .sizeDp(20));
                textView.setText(item.getString(NetValue.MLIBRARY_LIBRARYNAME));
                switch (item.getString(NetValue.LIBRARY_CLASSTYPE)){
                    case NetValue.LIBRARY_CLASSTYPE_ANDROID:
                        setIconDrawable(textView, MaterialDesignIconic.Icon.gmi_android);
                        break;
                    case NetValue.LIBRARY_CLASSTYPE_APP:
                        setIconDrawable(textView, MaterialDesignIconic.Icon.gmi_apps);
                        break;
                    case NetValue.LIBRARY_CLASSTYPE_QD:
                        setIconDrawable(textView, MaterialDesignIconic.Icon.gmi_language_javascript);
                        break;
                    case NetValue.LIBRARY_CLASSTYPE_JIAOCHENG:
                        setIconDrawable(textView, MaterialDesignIconic.Icon.gmi_book);
                        break;
                }
            }
        };
        baseQuickAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(LibraryListActivity.this, WebViewActivityy.class);
                intent.putExtra(NetValue.MLIBRARY_LIBRARYNAME,datas.get(position).getString("libraryName"));
                intent.putExtra(NetValue.MLIBRARY_CLASSTYPE,datas.get(position).getString("classType"));
                intent.putExtra(NetValue.MLIBRARY_LIBRARYURL,datas.get(position).getString("libraryUrl"));
                startActivity(intent);
            }
        });
        baseQuickAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, final int position) {
                switch (view.getId()){
                    case R.id.img_desc:
                        final String desc = datas.get(position).getString("desc");
                        new MaterialDialog.Builder(LibraryListActivity.this)
                                .title(datas.get(position).getString("libraryName"))
                                .icon(new IconicsDrawable(LibraryListActivity.this)
                                        .color(ThemeUtils.getThemeColor(LibraryListActivity.this, R.attr.colorPrimary))
                                        .icon(MaterialDesignIconic.Icon.gmi_android)
                                        .sizeDp(20))
                                .content(desc)
                                .negativeText("关闭")
                                .positiveText("复制")
                                .onPositive(new MaterialDialog.SingleButtonCallback() {
                                    @Override
                                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                        ClipboardManager clipboardManager = (ClipboardManager)LibraryListActivity.this.getSystemService(Context.CLIPBOARD_SERVICE);
                                        //创建ClipData对象
                                        ClipData clipData = ClipData.newPlainText("simple text copy", desc);
                                        //添加ClipData对象到剪切板中
                                        clipboardManager.setPrimaryClip(clipData);
                                        Toasty.info(getApplicationContext(), "已复制到剪切板", Toast.LENGTH_SHORT, true).show();
                                    }
                                })
                                .show();
                        break;
                }
            }
        });
        baseQuickAdapter.isFirstOnly(false);
        baseQuickAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
        recyclerView.setAdapter(baseQuickAdapter);
    }

    @Override
    public void onRefresh() {
        initNetDatas();
    }
    @Subscribe
    public void onEvent(SkinChangeEvent event){
        if(baseQuickAdapter!=null){
            baseQuickAdapter.notifyDataSetChanged();
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}
